const vscode = acquireVsCodeApi();

window.addEventListener('message', event => {
    if ("predictions" in event.data) {
        predictions= event.data.predictions;
        let markHtml = marked.parse(predictions);
        setCode(markHtml);
    } else if ("colorTheme" in event.data){
        let colorTheme=event.data.colorTheme;
        var page = colorTheme.indexOf("Dark")!==-1? "white":'black'; // white, black
        document.body.className = page;
    }
});

function generate(){
    if(!document.getElementById('c-button').className) {return ;}
    html = editor.$textElem.html();
    html = html.substring(0,html.indexOf('<tr>'))+html.substring(html.indexOf('</tr>')+5);
    vscode.postMessage({"table":html,"pseudoCode":document.getElementById('pseudoCode').value});
}
//伪代码
document.getElementById('pseudoCode').addEventListener('keyup',function(e){
    var value = e.target.value;
    if(value && editor.$textElem.text()){
        document.getElementById('c-button').className = 'active';
    }else{
        document.getElementById('c-button').className = '';
    }
});

document.addEventListener('contextmenu', function(e) {
    e.preventDefault(); //取消右击事件
});

// const { createEditor, createToolbar } = window.wangEditor;
// const editorConfig = {
//     placeholder: 'paste here...',
//     onChange(editor) {
//       const html = editor.getHtml();
//       console.log('editor content', html);
//       if(html && document.getElementById('formula').value){
//         document.getElementById('c-button').className = 'active';
//       }else{
//         document.getElementById('c-button').className = '';
//       }
//     }
// };

// const editor = createEditor({
//     selector: '#editor-container',
//     html: '<p><br></p>',
//     config: editorConfig,
//     mode: 'simple', // or 'simple'
// });

// const toolbar = createToolbar({
//     editor,
//     selector: '#toolbar-container',
//     config: {
//         toolbarKeys:[]
//     },
//     mode: 'default', // or 'simple'
// });
const E = window.wangEditor;
const { createEditor, createToolbar } = E;
window.editor = new E("#editor-container");
editor.config.menus = [];
editor.config.placeholder = '请插入表格';
editor.create();

editor.config.onchange = function (newHtml) {
    newHtml = newHtml.substring(0,newHtml.indexOf('<tr>'))+newHtml.substring(newHtml.indexOf('</tr>')+5);
    console.log(JSON.stringify(newHtml));
    if(newHtml && document.getElementById('pseudoCode').value){
        document.getElementById('c-button').className = 'active';
    }else{
        document.getElementById('c-button').className = '';
    }
};